package core
{
	import core.view.MagicEditView;
	
	import flash.display.Bitmap;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.system.Security;
	
	import org.ais.event.IEvent;
	import org.ais.system.Ais;
	import org.aisy.display.USprite;

	[SWF(width=960, height=560)]
	public class Main extends USprite
	{
		[Embed(source="../image/bg.jpg")]
		protected var bgCls:Class;
		
		public function Main()
		{
			try {
				Security.allowDomain("*");
				Security.allowInsecureDomain("*");
			} catch (error:Error) {}
			if (!Ais.IMain) {
				Ais.IMain = this;
				connect(this);
			}
		}
		
		public function connect(im:*):void
		{
			im.stage.align = StageAlign.TOP_LEFT;
			im.stage.scaleMode = StageScaleMode.NO_SCALE;
			
			var url:String = loaderInfo.parameters["url"];
			if (!url) url = "magic/e";
			MagicEditView.getInstance().load(url);
			
			if (this !== im) im.addChild(this);
			
			Ais.IMain.stage.addEventListener(Event.RESIZE, __stageHandler);
			Ais.IMain.stage.dispatchEvent(new Event(Event.RESIZE));
			
			url = null;
			im = null;
		}
		
		protected function __import():void
		{
			IEvent;
		}
		
		protected function __stageHandler(e:Event):void
		{
			var im:* = Ais.IMain;
			im.graphics.clear();
			im.graphics.beginBitmapFill((new bgCls() as Bitmap).bitmapData);
			im.graphics.drawRect(0, 0, Ais.IMain.stage.stageWidth, Ais.IMain.stage.stageHeight);
			im.graphics.endFill();
			im = null;
		}
		
		override public function clear():void
		{
			Ais.IMain.stage.removeEventListener(Event.RESIZE, __stageHandler);
			super.clear();
			bgCls = null;
		}
	}
}